﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Categorys
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class CategoryLanguageDAO
    {

        #region general

        private static CategoryLanguage LoadObj(IDataReader datareader)
        {
            CategoryLanguage obj = new CategoryLanguage();
            obj.CLID = MpDataHelp.GetInt(datareader, "CLID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.LanguageCode = MpDataHelp.GetString(datareader, "LanguageCode");
            obj.CategoryName = MpDataHelp.GetString(datareader, "CategoryName");
            obj.CategoryDESC = MpDataHelp.GetString(datareader, "CategoryDESC");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            return obj;
        }

        #endregion

        public static int CategoryLanguage_Add(CategoryLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_CategoryLanguage_Add");
            db.AddOutParameter(dbCommand, "CLID", DbType.Int32, obj.CLID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "CategoryName", DbType.String, obj.CategoryName);
            db.AddInParameter(dbCommand, "CategoryDESC", DbType.String, obj.CategoryDESC);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@CLID"));
            return -1;
        }
        public static int CategoryLanguage_Update(CategoryLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_CategoryLanguage_Update");
            db.AddInParameter(dbCommand, "CLID", DbType.Int32, obj.CLID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "CategoryName", DbType.String, obj.CategoryName);
            db.AddInParameter(dbCommand, "CategoryDESC", DbType.String, obj.CategoryDESC);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int CategoryLanguage_Delete(int CLID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_CategoryLanguage_Delete");
            db.AddInParameter(dbCommand, "CLID", DbType.Int32, CLID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<CategoryLanguage> CategoryLanguage_Select(int CLID, int CategoryID, string LanguageCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_CategoryLanguage_Select");
            db.AddInParameter(dbCommand, "CLID", DbType.Int32, CLID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, LanguageCode);
            List<CategoryLanguage> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<CategoryLanguage>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}

